package com.next

class FlexFieldInstance
{
    FlexFieldCategory category
    FlexField field
    String value

    Account account
    Contact contact
    Activity activity
    Asset asset
    Campaign campaign
    Contract contract
    Issue issue
    Lead lead
    Opportunity opportunity

    User createBy
    User modifyBy
    User user

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    static constraints = {
        value maxSize: 256, nullable: true, blank: true

        account nullable: true, blank: true
        contact nullable: true, blank: true
        activity nullable: true, blank: true
        asset nullable: true, blank: true
        campaign nullable: true, blank: true
        contract nullable: true, blank: true
        issue nullable: true, blank: true
        lead nullable: true, blank: true
        opportunity nullable: true, blank: true

        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
        user nullable: true, blank: true

        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
    }

    static mapping = {
        sort 'category', 'field'
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
        if (!user)
        {
            user = createBy
        }
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }

    String toString()
    {
        value
    }
}
